import { CONVERT_TYPE } from "../convertable/Consts"
import { BaseUtil } from "../convertable/BaseUtil"

export class ObjectArray extends BaseUtil {
  constructor(array) {
    super()
    this.array = array || []
    this.keyMap = {}

    this.type = CONVERT_TYPE.N
  }

  build() {
    const { array = [], excludeKeys = [], excludeTypeKeys = [] } = this
    const resultArray = []
    array.map(row => {
      const obj = {}
      Object.entries(row).map(([key, value]) => {
        if (!excludeKeys.includes(key)) {
          let _key = key
          _key = this.keyMap[key] || _key

          if (this.type !== CONVERT_TYPE.N && !excludeTypeKeys.includes(key)) {
            _key = this._toTypedKey(_key)
          }
          obj[_key] = value
        }
      })
      resultArray.push(obj)
    })

    return resultArray
  }

  keyAsName(map = {}) {
    this.keyMap = map
    return this
  }
}
